generate_plots <- function(df, yref, ylab, xlab, ybreaks, xbreaks, file_path, type = "") {
  
  if(type == "product"){
    print(df)
  }
  if (unique(df$Model) == "RCL23") {
    df <- df %>% mutate(
      Instrument = factor(
        Instrument,
        levels = c(
          "miller and weinberg instruments",
          "strongly excluded instruments",
          "partial residualization (products)"
        ),
        labels = c(
          "Baseline",
          "Strongly excluded (price coefficient)",
          "Choice-set residualization"
        )
      )
    )
  } else if (type == "product") {
    df <- df %>% mutate(
      Instrument = factor(
        Instrument,
        levels = c(
          "miller and weinberg instruments",
          "strongly excluded instruments",
          "product-level residualization"
        ),
        labels = c(
          "Baseline",
          "Strongly excluded",
          "Product-level residualization"
        )
      )
    )
  }else {
    df <- df %>% mutate(
      Instrument = factor(
        Instrument,
        levels = c(
          "miller and weinberg instruments",
          "strongly excluded instruments",
          "partial residualization (products)",
          "product-level residualization"
        ),
        labels = c(
          "Baseline",
          "Strongly excluded",
          "Choice-set residualization",
          "Product-level residualization"
        )
      )
    )
  }

  if(type == "product"){
    main_color_scheme = c("#66c2a5", "#fc8d62","#800080")
    main_shape_scheme = c(23, 22, 24)
  }else {
    main_color_scheme = c("#66c2a5", "#fc8d62", "#8da0cb","#800080")
    main_shape_scheme = c(23, 22, 21, 24)
  }
  
  names(main_color_scheme) <- names(main_shape_scheme) <- levels(df$Instrument)
  color_scheme = main_color_scheme[names(main_color_scheme) %in% df$Instrument]
  shape_scheme = main_shape_scheme[names(main_shape_scheme) %in% df$Instrument]
  
  yref_label <- ifelse(yref$Outcome == "mad_own_elasticity", "Endogeneity error",
                     ifelse(yref$Outcome == "mean_own_elasticity", "Endogeneity bias", ""))
  
  ggplot(df, aes(x = Distance, y = median)) +
    geom_hline(yintercept = yref$median, linewidth = 0.25, linetype = "dashed") + 
    geom_hline(yintercept = 0, linewidth = 0.25) +
    geom_line(
      aes(group = Instrument,
          linetype = "solid",
          color = Instrument),
      linewidth = 0.5, show.legend = FALSE
    ) +
    geom_errorbar(
      aes(ymin = p05, ymax = p95,
          color = Instrument,
          linetype = "solid"),
      width = (xbreaks[length(xbreaks)] - xbreaks[1]) / 10 / length(unique(df$Distance)), 
      show.legend = FALSE) +
    geom_point(
      aes(fill = Instrument, shape = Instrument), size = 2,
      color = "black", stroke = 0.5
    ) +
    scale_shape_manual(
      name = "Instrument",
      values = shape_scheme,
      guide = guide_legend(override.aes = list(fill = color_scheme))
    ) +
    scale_fill_manual(name = "Instrument", values = color_scheme, guide = "none") +
    scale_color_manual(name = "Instrument", values = color_scheme, guide = "none") +
    scale_x_continuous(breaks = xbreaks, limits = c(xbreaks[1]-xbreaks[length(xbreaks)]*0.01, xbreaks[length(xbreaks)]*1.01)) +
    coord_cartesian(xlim =  c(xbreaks[1], xbreaks[length(xbreaks)]), clip = "off") +
    scale_y_continuous(breaks = ybreaks) +
    coord_cartesian(ylim = c(ybreaks[1], ybreaks[length(ybreaks)])) +
    labs(x = xlab, y = ylab) +
    theme_bw() +
    theme(
      legend.direction      = "vertical",
      legend.box.spacing    = unit(0.1, "pt"),
      legend.title.align    = 0.5,
      legend.box.background = element_rect(colour = "black", size = 0.8),
      legend.title          = element_text(size = 14, face = "bold"),
      legend.text           = element_text(size = 14),
      legend.position = c(0,1),
      legend.justification = c(-0.1, 1.1), 
      axis.title            = element_text(size = 14),
      axis.text             = element_text(size = 12),
      axis.ticks.y          = element_blank(),
      panel.grid.major      = element_blank(),
      panel.grid.minor      = element_blank(),
      panel.background      = element_blank(),
      axis.line             = element_line(colour = "black"),
      panel.border          = element_blank()
    ) + 
    annotate(geom = "text", label = yref_label, 
             x = xbreaks[length(xbreaks)], y = yref$median + 0.075, size = 5, hjust = 1)
  
  ggsave(file_path, width = 8, height = 5, units = "in")
  
}
